/**
 * 作者: wgx
 * 日期: 2025-03-31 10:55:19
 * 备注: 用户已解锁的餐厅
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserRestaurantRoom;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserRestaurantRoomModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.restaurant_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int restaurantId; // 餐厅id
    protected int level; // 餐厅等级初始1级

    public void reset() {
    }

    protected String getKey() {
        return "UserRestaurantRoom." + userId + "." + restaurantId;
    }

    protected String getListKey() {
        return "UserRestaurantRoom." + userId;
    }

    protected String getListItemKey() {
        return restaurantId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_restaurant_room"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("restaurantId", restaurantId); // 餐厅id
        obj.put("level", level); // 餐厅等级初始1级
        return obj;
    }

    public PBUserRestaurantRoom.Builder takeInitBuilder() {
        PBUserRestaurantRoom.Builder sb = PBUserRestaurantRoom.newBuilder();
        if (restaurantId != 0) {
            sb.setRestaurantId(restaurantId);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        return sb;
    }

    public PBUserRestaurantRoom.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserRestaurantRoom.Builder sb = PBUserRestaurantRoom.newBuilder();
        if (isFieldUpdate("restaurantId")) {
            sb.setRestaurantId(restaurantId);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getRestaurantId() {
        return restaurantId;
    }

    public void putRestaurantId(int restaurantId) {
        if (this.restaurantId != restaurantId) {
            this.restaurantId = restaurantId;
            updateRedisHashField(getKey(), "restaurantId", this.restaurantId);
        }
    }

    public void addRestaurantId(int value) {
        this.restaurantId += value;
        updateRedisHashField(getKey(), "restaurantId", this.restaurantId);
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

}